package package1;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

public class TestForLoginSystem {
	/**
	 *  simulation of cmd function, to be finished
	 * 1. robust check for cmd command  
	 * 2. execution for cmd command
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
//		loginSystem();
		showFileTree();
	}

	private static void showFileTree() throws IOException {
		String filePath = "";
		java.util.Scanner s = new Scanner(System.in);
		String consoloCMD; String runCMD; String file;
		String currentAddress = "";
		while(true)
		{
			System.err.print("input your command:");
			consoloCMD = s.nextLine();
			checkCMD(consoloCMD);
			runCMD = dealWithCMD(consoloCMD);
			if(runCMD.equals(".."))
			{
				//back to previous higher level of catalogue
			}
			else if(runCMD.equalsIgnoreCase(""))
			{
				//
			}
				
			
			filePath = currentAddress + runCMD;
			if(new File(filePath).isDirectory())
				currentAddress = listFiles(filePath);
			else
				java.awt.Desktop.getDesktop().open(new File(filePath));
			System.err.println("currentaddress:"+currentAddress);
		}
		
		
	}

	private static String dealWithCMD(String consoloCMD) {
		String temp;
		temp = consoloCMD.trim();
		temp = temp.substring(temp.indexOf("cd")+2).trim();
		return temp;
	}

	private static void checkCMD(String cmd) {
		String spaces = "[\\s]*";
		String wordsAndDigitals = "[\\w]*";
		String dots = "[.]*";
		cmd = "   cd aaa";
		LinkedList<String> checkRegList = new LinkedList<String>();
		checkRegList.add(spaces+"cd"+spaces+wordsAndDigitals+spaces);
		checkRegList.add(spaces+"cd"+spaces+dots+spaces);
		boolean checkPassed = false;
		for(String c:checkRegList)
			if(cmd.matches(c)){
				checkPassed = true;
				break;
			}
		if(checkPassed)
			System.err.println(cmd +" check succeeded!");
		else
			System.err.println(cmd +" check failed!");
			String check = "[\\s]*cd";
		if(!cmd.matches(check))
			System.err.println(cmd +" not pass check!");
	}

	private static String listFiles(String filePath) {
		System.err.println("filePath:"+filePath);
		if(!filePath.endsWith("/"))	filePath = filePath+"/";
		File f1 = new File(filePath);
		for(File f:f1.listFiles())
			System.err.println("\t"+f.getName());
		return filePath;
	}

	private static void loginSystem() {
		HashMap<String,String>credentialMap = new HashMap<String,String>();
		credentialMap.put("a", "1");
		java.util.Scanner s = new Scanner(System.in);
		String username; String password;
		System.err.println("hello! welcome to login system!");
		boolean passwordCheck = false;
		while(true){
			if(passwordCheck){
				System.err.println("checked passed");
				break;
			}
			System.err.println("please input your username");
			username = s.nextLine();
			if(!credentialMap.keySet().contains(username)){
				System.err.print("the username does not exist, ");
				continue;
			}
			String sen = "please input password";
			while(true){
				System.err.println(sen);
				password = s.nextLine();
				if(password.equalsIgnoreCase("back"))
					break;
				if(credentialMap.get(username).equals(password)){
					System.err.println("match, login...");
					passwordCheck = true;
					break;
				}
				else
				{
					System.err.print("password not match for username \""+username+"\", ");
					sen = "please input password again or type 'back' to input another username";	
				}
				continue;
			}
		}
	}
}
